package com.yang.moyu.meta.system.controller;

import com.yang.moyu.meta.common.param.BaseParam;
import com.yang.moyu.meta.common.util.ApiResult;
import com.yang.moyu.meta.common.util.RandomValidateCodeUtil;
import com.yang.moyu.meta.framework.aop.annotation.Log;
import com.yang.moyu.meta.framework.aop.annotation.OperationLogIgnore;
import com.yang.moyu.meta.system.params.LoginParam;
import com.yang.moyu.meta.system.params.ResetPwdParam;
import com.yang.moyu.meta.system.service.LoginService;
import com.yang.moyu.meta.system.service.impl.LoginServiceImpl;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

/**
 * Create by 10296 on 2021/12/15
 *
 * @Description:
 */
@RestController
@RequestMapping("/systemApi")
@Api(value = "系统登录API", tags = {"系统登录"})
public class LoginController {

    private static Logger logger = LoggerFactory.getLogger(LoginController.class);
    @Autowired
    private LoginService loginService;


    @Log("系统登录")
    @PostMapping("/login")
    @ApiOperation(value = "登录", notes = "系统用户登录")
    public ApiResult login(@Validated @RequestBody LoginParam loginParam, HttpServletResponse response) {
        logger.info(">>>>>>>>>>>>>登录请求参数>>>>>>>>>>>loginParam:{}", loginParam);
        return loginService.login(loginParam, response);
    }

    @OperationLogIgnore
    @PostMapping("/generateVerifyCode")
    @ApiOperation(value = "生成验证码", notes = "生成验证码")
    public void generateVerifyCode(HttpServletRequest request, HttpServletResponse response) {
        try {
            response.setContentType("image/jpeg");//设置相应类型,告诉浏览器输出的内容为图片
            response.setHeader("Pragma", "No-cache");//设置响应头信息，告诉浏览器不要缓存此内容
            response.setHeader("Cache-Control", "no-cache");
            response.setDateHeader("Expire", 0);
            RandomValidateCodeUtil randomValidateCode = new RandomValidateCodeUtil();
            randomValidateCode.getRandCode(request, response);//输出验证码图片方法
        } catch (Exception e) {
            logger.error("获取验证码失败>>>>", e);
        }

    }

    /**
     * 验证token
     *
     * @param param    参数
     * @param response 返回
     * @return ""
     */
    @Log("验证Token")
    @PostMapping("/verifyAuthToken")
    @ApiOperation(value = "验证Token", notes = "验证Token")
    public ApiResult verifyAuthToken(@Validated @RequestBody BaseParam param, HttpServletResponse response) {

        logger.info(">>>>>>>>>>>>verifyAuthToken" + param.getToken());
        return loginService.verifyAuthToken(param.getToken());
    }

    /**
     * 重置密码
     *
     * @param resetPwdParam 参数
     * @param response      ''
     * @return 对象
     * @throws Exception e
     */
    @Log("重置密码")
    @PostMapping("/reset")
    @ApiOperation(value = "重置密码", notes = "重置密码")
    public ApiResult<String> reset(@Validated @RequestBody ResetPwdParam resetPwdParam, HttpServletResponse response) {
        logger.info(">>>>>>>>>>>>>>>>>>>>>>>>>重置密码>>>>>" + resetPwdParam);
        return loginService.resetPassword(resetPwdParam);
    }

    /**
     * 忘记密码
     *
     * @param loginParam 参数
     * @param response   ''
     * @return 对象
     * @throws Exception e
     */
    @Log("忘记密码")
    @PostMapping("/forget")
    @ApiOperation(value = "忘记密码", notes = "忘记密码")
    public ApiResult<String> forgetPassword(@Validated @RequestBody LoginParam loginParam, HttpServletResponse response) throws Exception {
        logger.info(">>>>>>>>>>>>>>>>>.forgetPassword:::" + loginParam);

        return ApiResult.ok("密码修改成功");
    }


    /**
     * 退出登录
     *
     * @param request
     * @param response
     * @return
     * @throws Exception
     */
    @PostMapping("/logout")
    @OperationLogIgnore
    public ApiResult<String> logout(HttpServletRequest request, HttpServletResponse response) throws Exception {

        loginService.logout(request);


        return ApiResult.ok("退出成功");
    }


}
